package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.TIME;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.RegexpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.widget.EditTextWithClear;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * ================================================
 * 项目名称：bigdata
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2016/12/6 14:13
 * 修改备注：
 * ================================================
 */
public class ForgetPasswordStepOneActivity extends BaseActivity {

    @Bind(R.id.forget_pd_et_mobile)
    EditTextWithClear forgetPdEtMobile;
    @Bind(R.id.forget_pd_et_auth_code)
    EditTextWithClear forgetPdEtAuthCode;
    @Bind(R.id.forget_pd_bt_auth_code)
    Button forgetPdBtAuthCode;
    @Bind(R.id.forget_pd_bt_next)
    Button forgetPdBtNext;

    private String mStrMobile;
    private String mStrAuthCode;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_forget_password_step_one);
        setTitle("忘记密码");
    }

    @Override
    public void initMember() {

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SEND_PHONE_CODE_UPDATE_PWD_SUCCESS:
                        Utils.startForbitSendVerifyCode(ForgetPasswordStepOneActivity.this,
                                mHandler, TIME.SEND_CHECKCODE_TIME, null);
                        break;
                    case MSG.MSG_SEND_PHONE_CODE_UPDATE_PWD_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_TIP:
                        if (isFinishing())
                            return;
                        forgetPdBtAuthCode.setClickable(false);
                        forgetPdBtAuthCode.setTextColor(Color.parseColor("#b3b3b3"));
                        forgetPdBtAuthCode.setText(String.valueOf(msg.obj));
                        forgetPdBtAuthCode.setBackgroundResource(R.drawable.shape_bt_auth_code_disable);
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_END:
                        if (isFinishing())
                            return;
                        forgetPdBtAuthCode.setClickable(true);
                        forgetPdBtAuthCode.setTextColor(getResources().getColor(R.color.app_color));
                        forgetPdBtAuthCode.setText("发送验证码");
                        forgetPdBtAuthCode.setBackgroundResource(R.drawable.shape_rect_app_color);
                        break;
                    case MSG.MSG_SAFE_CHECK_FOR_UP_PWD_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SAFE_CHECK_FOR_UP_PWD_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {
        GotoUtil.gotoActivity(this, ForgetPasswordStepTwoActivity.class, true, "mobilePhone", mStrMobile);
    }

    @Override
    public void rightOnClick() {

    }


    @OnClick({R.id.forget_pd_bt_auth_code, R.id.forget_pd_bt_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_pd_bt_auth_code:
                getAuthCode();
                break;
            case R.id.forget_pd_bt_next:
                next();
                break;
        }
    }

    /**
     * 获取输入内容 点击下一步
     */
    private void next() {

        mStrMobile = StringUtil.getEditText(forgetPdEtMobile);
        mStrAuthCode = StringUtil.getEditText(forgetPdEtAuthCode);

        if (StringUtil.isStrEmpty(mStrMobile)) {
            showToast("手机号不能为空");
            return;
        }

        if (!RegexpUtil.isRegexpValidate(mStrMobile, RegexpUtil.MOBILE_REGEXP)) {
            showToast("请输入合法手机号");
            return;
        }

        if (StringUtil.isStrEmpty(mStrAuthCode)) {
            showToast("请填写验证码");
            return;
        }

        BusinessAccount.safeCheckForUpPwd(this, mStrMobile, mStrAuthCode, mHandler);
    }

    /**
     * 获取验证码
     */
    private void getAuthCode() {
        mStrMobile = forgetPdEtMobile.getText().toString();
        if (StringUtil.isStrEmpty(mStrMobile)) {
            showToast("手机号不能为空");
            return;
        }

        if (!RegexpUtil.isRegexpValidate(mStrMobile, RegexpUtil.MOBILE_REGEXP)) {
            showToast("请输入合法手机号");
            return;
        }


        BusinessAccount.sendPhoneCodeUpdatePwd(this, mStrMobile, mHandler);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
